//
//  UIKitExampleStackViewController.m
//  MasonryExample
//
//  Created by 山刘 on 2020/6/12.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "UIKitExampleStackViewController.h"
#import <objc/runtime.h>

@interface UIKitExampleStackViewController ()
@property (weak, nonatomic) IBOutlet UITextView *logTextView;
@property (weak, nonatomic) IBOutlet UITextField *spacingField;
@property(nonatomic,assign) UILayoutConstraintAxis axis;
@property(nonatomic,assign) UIStackViewDistribution distribution;
@property(nonatomic,assign) UIStackViewAlignment alignment;
@property(nonatomic,strong) NSMutableString *logString;

@property(nonatomic,strong) UIStackView *stackView;
@end

@implementation UIKitExampleStackViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"UIStackView Demo";
        self.axis = UILayoutConstraintAxisHorizontal;
        self.distribution = UIStackViewDistributionFill;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //self.spacingField.keyboardType = UIKeyboardTypeNumberPad;
    self.spacingField.returnKeyType = UIReturnKeyDone;
    
    self.logTextView.editable = NO;
    
    //_stackView = [[UIStackView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(self.logTextView.frame),KScreenWidth,100)];
    _stackView = [[UIStackView alloc] init];
    _stackView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_stackView];
    [self.stackView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(kPadding);
        make.right.lessThanOrEqualTo(kPadding);
        make.top.equalTo(self.logTextView.mas_bottom);
        make.height.equalTo(@150);
    }];
    
//        {
//            UIView *subView1 = [LayoutUtils createView];
//            UIView *subView2 = [LayoutUtils createView];
//            UIView *subView3 = [LayoutUtils createView];
//
//            subView1.backgroundColor = UIColor.redColor;
//            subView2.backgroundColor = UIColor.greenColor;
//            subView3.backgroundColor = UIColor.blueColor;
//
//            //subView1.frame = CGRectMake(0, 0, 40, 20);
//            //subView2.frame = CGRectMake(0, 0, 100, 70);
//            //subView3.frame = CGRectMake(0, 0, 70, 40);
//
//            subView1.tag = 1;
//            subView2.tag = 2;
//            subView3.tag = 3;
//
//            [_stackView addArrangedSubview:subView1];
//            [_stackView addArrangedSubview:subView2];
//            [_stackView addArrangedSubview:subView3];
//        }
    {
        UILabel *subView1 = [LayoutUtils fixedLabelWithText:@"40, 20"];
        UILabel *subView2 = [LayoutUtils fixedLabelWithText:@"100, 70"];
        UILabel *subView3 = [LayoutUtils fixedLabelWithText:@"70, 40"];

        subView1.backgroundColor = UIColor.redColor;
        subView2.backgroundColor = UIColor.greenColor;
        subView3.backgroundColor = UIColor.blueColor;

        //subView1.frame = CGRectMake(0, 0, 40, 20);
        //subView2.frame = CGRectMake(0, 0, 100, 70);
        //subView3.frame = CGRectMake(0, 0, 70, 40);
        
        [subView1 makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(@(CGSizeMake(40,150)));
        }];
        [subView2 makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(@(CGSizeMake(100, 150)));
        }];
        [subView3 makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(@(CGSizeMake(70, 150)));
        }];

        subView1.tag = 1;
        subView2.tag = 2;
        subView3.tag = 3;

        [_stackView addArrangedSubview:subView1];
        [_stackView addArrangedSubview:subView2];
        [_stackView addArrangedSubview:subView3];
    }
}

-(void)updateViewConstraints{
    [super updateViewConstraints];
}

- (IBAction)log{
    [self.view endEditing:YES];
    
    self.logString = [NSMutableString string];
    
    [self.logString appendString:@"axis:  "];
    if (self.axis == UILayoutConstraintAxisHorizontal) [self.logString appendString:@"UILayoutConstraintAxisHorizontal"];
    if (self.axis == UILayoutConstraintAxisVertical) [self.logString appendString:@"UILayoutConstraintAxisVertical"];
    [self.logString appendString:@"\n"];
    
    [self.logString appendString:@"distribution:  "];
    if (self.distribution == UIStackViewDistributionFill) [self.logString appendString:@"UIStackViewDistributionFill"];
    if (self.distribution == UIStackViewDistributionFillEqually) [self.logString appendString:@"UIStackViewDistributionFillEqually"];
    if (self.distribution == UIStackViewDistributionFillProportionally) [self.logString appendString:@"UIStackViewDistributionFillProportionally"];
    if (self.distribution == UIStackViewDistributionEqualSpacing) [self.logString appendString:@"UIStackViewDistributionEqualSpacing"];
    if (self.distribution == UIStackViewDistributionEqualCentering) [self.logString appendString:@"UIStackViewDistributionEqualCentering"];
    [self.logString appendString:@"\n"];
    
    [self.logString appendString:@"alignment:  "];
    if (self.alignment == UIStackViewAlignmentFill) [self.logString appendString:@"UIStackViewAlignmentFill"];
    if (self.alignment == UIStackViewAlignmentLeading) [self.logString appendString:@"UIStackViewAlignmentLeading"];
    if (self.alignment == UIStackViewAlignmentTop) [self.logString appendString:@"UIStackViewAlignmentTop"];
    if (self.alignment == UIStackViewAlignmentFirstBaseline) [self.logString appendString:@"UIStackViewAlignmentFirstBaseline"];
    if (self.alignment == UIStackViewAlignmentCenter) [self.logString appendString:@"UIStackViewAlignmentCenter"];
    if (self.alignment == UIStackViewAlignmentTrailing) [self.logString appendString:@"UIStackViewAlignmentTrailing"];
    if (self.alignment == UIStackViewAlignmentBottom) [self.logString appendString:@"UIStackViewAlignmentBottom"];
    if (self.alignment == UIStackViewAlignmentLastBaseline) [self.logString appendString:@"UIStackViewAlignmentLastBaseline"];
    [self.logString appendString:@"\n"];
    
    [self.logString appendFormat:@"size: %@ ",NSStringFromCGSize([UIScreen mainScreen].bounds.size)];
    [self.logString appendString:@"\n"];
    
    self.stackView.axis = self.axis;
    self.stackView.distribution = self.distribution;
    self.stackView.alignment = self.alignment;
    self.stackView.spacing = (self.spacingField.text && self.spacingField.text.length > 0) ? [self.spacingField.text floatValue] : 0;
    [UIView animateWithDuration:1.0 animations:^{
        [self.stackView layoutIfNeeded];
    }];
    
    for (UIView *object in self.stackView.arrangedSubviews) {
        [self.logString appendFormat:@"tag:%zd,frame:%@",object.tag,NSStringFromCGRect(object.frame)];
        [self.logString appendString:@"\n"];
    }
    
    self.logTextView.text = self.logString;
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
#pragma mark - axis
- (IBAction)HorizontalClick:(id)sender { self.axis = UILayoutConstraintAxisHorizontal;[self.view endEditing:YES];}
- (IBAction)VerticalClick:(id)sender { self.axis = UILayoutConstraintAxisVertical;[self.view endEditing:YES];}

#pragma mark - axis

- (IBAction)FillClick:(id)sender { self.distribution = UIStackViewDistributionFill;[self.view endEditing:YES];
    for (UIView *object in self.stackView.arrangedSubviews) {
        [object remakeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@100);
        }];
    }
}
- (IBAction)FillEquallyClick:(id)sender { self.distribution = UIStackViewDistributionFillEqually;[self.view endEditing:YES];}
- (IBAction)FillProportionallyClick:(id)sender { self.distribution = UIStackViewDistributionFillProportionally;[self.view endEditing:YES];}
- (IBAction)EqualSpacingClick:(id)sender { self.distribution = UIStackViewDistributionEqualSpacing;[self.view endEditing:YES];}
- (IBAction)EqualCenteringClick:(id)sender { self.distribution = UIStackViewDistributionEqualCentering; [self.view endEditing:YES];}

#pragma mark - alignment
- (IBAction)alignmentFillClick:(id)sender { self.alignment = UIStackViewAlignmentFill;[self.view endEditing:YES];}
- (IBAction)LeadingTopClick:(id)sender {
    if (self.axis == UILayoutConstraintAxisVertical) {
        self.alignment = UIStackViewAlignmentLeading;
    }else{
        self.alignment = UIStackViewAlignmentTop;
    }
    [self.view endEditing:YES];
}
- (IBAction)FirstBaselineClick:(id)sender { self.alignment = UIStackViewAlignmentFirstBaseline;[self.view endEditing:YES];}
- (IBAction)CenterClick:(id)sender { self.alignment = UIStackViewAlignmentCenter;[self.view endEditing:YES];}
- (IBAction)TrailingBottomClick:(id)sender {
    if (self.axis == UILayoutConstraintAxisVertical) {
        self.alignment = UIStackViewAlignmentTrailing;
    }else{
        self.alignment = UIStackViewAlignmentBottom;
    }
    [self.view endEditing:YES];
}
- (IBAction)LastBaselineClick:(id)sender { self.alignment = UIStackViewAlignmentLastBaseline;[self.view endEditing:YES];}
@end
